/****************************************************************************
 * Copyright 2009 Huascar A. Sanchez                                        *
 *                                                                          *
 * Licensed under the Apache License, Version 2.0 (the "License");          *
 * you may not use this file except in compliance with the License.         *
 * You may obtain a copy of the License at                                  *
 *                                                                          *
 *     http://www.apache.org/licenses/LICENSE-2.0                           *
 *                                                                          *
 * Unless required by applicable law or agreed to in writing, software      *
 * distributed under the License is distributed on an "AS IS" BASIS,        *
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
 * See the License for the specific language governing permissions and      *
 * limitations under the License.                                           *
 ****************************************************************************/
package com.gotobject.filter;

import com.gotobject.filter.spi.Variant;
import org.junit.Test;

import java.util.Arrays;
import java.util.List;

import static com.gotobject.filter.FilterQ.from;
import static com.gotobject.filter.common.Aggregations.count;
import static com.gotobject.filter.common.Iterables.asList;
import static com.gotobject.filter.common.Transformations.indexed;
import static com.gotobject.filter.common.Transformations.minus;
import static com.gotobject.filter.common.Transformations.occurrence;
import static com.gotobject.filter.common.Transformations.plus;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;

/**
 * @author huascar.sanchez@gmail.com (Huascar A. Sanchez)
 */
public class ApplyTransformationTest {
    @Test
    public void testByIndexingIterables() throws Exception {
        final Integer[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
        final String[] strings = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };

        final List<String> r = Arrays.asList("five", "four", "one", "three", "nine", "eight", "six", "seven", "two", "zero");
        final Iterable<String>  result =  from(numbers).apply(indexed(strings));
        final List<String>      s      = asList(result);

        assertSame("should have same size", r.size(), s.size());
        for(String each : s){
            assertTrue("should contain element", r.contains(each));
        }
    }

    @Test
    public void testByUsingPlusTransformation() throws Exception {
        final Integer[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
        final List<Integer> r = Arrays.asList(9, 8, 5, 7, 13, 12, 10, 11, 6, 4);
        final Iterable<Integer> result =  from(numbers).apply(plus(4));
        final List<Integer> s = asList(result);

        assertSame("should have same size", r.size(), s.size());
        for(Integer each : s){
            assertTrue("should contain element", r.contains(each));
        }
    }

    @Test
    public void testByUsingMinusTransformation() throws Exception {
        final Integer[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
        final List<Integer> r = Arrays.asList(1, 0, -3, -1, 5, 4, 2, 3, -2, -4);
        final Iterable<Integer> result =  from(numbers).apply(minus(4));
        final List<Integer> s = asList(result);

        assertSame("should have same size", r.size(), s.size());
        for(Integer each : s){
            assertTrue("should contain element", r.contains(each));
        }
    }


    @Test
    public void testGrepLikeFunction(){
        final String[] lines = {"Huascar is filterq's author. Huascar who?", "Slap shop commercial. Slap shop what?"};
        final String[] words = {"Huascar", "Slap"};
        final Iterable<Variant<Integer>> o = from(lines).apply(occurrence(words));
        assertEquals("two instances of Huascar", 2, count(o));
    }

}
